package com.kaifamiao.wendao.dao;

import com.kaifamiao.wendao.entity.CTR;
import com.kaifamiao.wendao.entity.Customer;
import com.kaifamiao.wendao.entity.Level;
import com.kaifamiao.wendao.entity.Topic;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CTRDao extends BaseDao<CTR,Long>{
    private static final String INSERT_ONE="INSERT INTO t_ctr (cid,tid) VALUES (?,?)";
    private static final String SELECT_ALL="SELECT tid FROM t_ctr WHERE cid=?";
    private static final String DELETE_ONE="DELETE FROM t_ctr WHERE cid=? AND tid=?";
    @Override
    public boolean save(CTR ctr) {
        return false;
    }

    @Override
    public boolean update(CTR ctr) {
        return false;
    }

    @Override
    public boolean remove(Long id) {
        return false;
    }

    @Override
    public List<CTR> findAll() {
        return null;
    }

    @Override
    public CTR find(Long id) {
        return null;
    }

    public boolean save(Long cid,Long tid){
        try{
            return runner.update(INSERT_ONE,cid,tid)==1;
        }catch (SQLException e){
            throw new RuntimeException("保存失败", e);
        }
    }

    public List<Long> loadAll(Long id){
        ResultSetHandler<List<Long>> rsHandler = resultSet -> {
            List<Long> list=new ArrayList<>();
            while (resultSet.next()){
                list.add(resultSet.getLong("tid"));
            }
            return list;
        };
        try{
            return runner.query(SELECT_ALL,rsHandler,id);
        }catch (SQLException e){
            throw new RuntimeException("查询所有用户信息失败",e);
        }
    }
    public boolean remove(Long cid,long tid) {
        try {
            return runner.update(DELETE_ONE,cid,tid)==1;
        }catch (SQLException e){
            throw new RuntimeException("删除失败",e);
        }

    }
}
